from the_crypt.models import *
from django.contrib import admin



#
#================ EVENTS
#
admin.site.register(Event)

#
#================ PROJECT
#

class SequenceInline(admin.TabularInline):
    model = Sequence
    fk_name = 'project'
    extra = 0
    fields = ['name', 'status', 'order']
    
    can_delete = False
    

class LibInline(admin.TabularInline):
    model = Lib
    fk_name = 'project'
    extra = 0
    fields = ['name', 'status', 'description']
    readonly_fields = ['description']
    can_delete = False


class ProjectAdmin(admin.ModelAdmin):
    
    #-- Project List page
    
    list_display = ['name', 'label', 'description', 'status']
    list_editable = ['label', 'status']
    ordering = ['create_date']
    list_filter = ['create_date', 'priority',]
    date_hierarchy = 'create_date'
    search_fields = ['name', 'status__name', 'description']
    #filter_vertical = ['name']
    
    #-- Project Edit/Create page
    
    fieldsets = [
        (None,      {'fields': ['name', 'label', 'status', 'description']}),
        ('Tracking',{
                    'fields': [('priority', 'depend'), ('start_date', 'due_date')],
                    'classes': ['collapse']
                    }
        ),
    ]
    inlines = [SequenceInline,LibInline]
    
    # Affects Both
    # That would be neet:
    #formfield_overrides = {
    #    models.ForeignKey: {'widget': ForeignKey_WithKoolStatus},
    #}
    
    #-- Overrides
    
    def THIS_DONT_WORK_formfield_for_foreignkey(self, db_field, request, **kwargs):
        '''
        Available status fields depends on the current status.
        THIS DOES NOT WORK since we can't get the current object's status
        '''
        #NB: the current user is request.user
        print self.queryset.__doc__
        if db_field.name == "statusX":
            currentStatus = '???'
            qs = Status.objects.filter(after=currentStatus)
            # TODO: Is this needed or would be automatique:
            qs = qs.filter(pk=currentStatus.pk) # Adds it to the set... not obvious!
            kwargs["queryset"] = qs
        return super(ProjectAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
    
    def get_readonly_fields(self, request, obj=None):
        '''
        Set the name as read only unless we are creating
        a new one.
        '''
        readonly_fields = list(super(ProjectAdmin, self).get_readonly_fields(request, obj))
        #obj is None is case of creation page:
        if obj is not None:
            readonly_fields.append('name')
        return readonly_fields
    
    #-- Actions
    
    def dummy_action(self, request, queryset):
        #rows_updated = queryset.update(status='p')
        #if rows_updated == 1:
        #    message_bit = "1 story was"
        #else:
        #    message_bit = "%s stories were" % rows_updated
        self.message_user(request, "This action is just a dummy (ran on %i objects)." % len(queryset))
    actions = ['dummy_action']

admin.site.register(Project, ProjectAdmin)

#
#================ LIB
#
def LibInline(model):
    def configurer(klass):
        klass.model = model
        klass.fk_name = 'lib'
        klass.fields = ['name', klass.my_field, 'status', 'hasHair', 'hasCloth']
        klass.extra = 0
        klass.can_delete = False
        return klass
    
    return configurer

@LibInline(Char)
class CharInline(admin.TabularInline):
    my_field = 'family'

@LibInline(Set)
class SetInline(admin.TabularInline):
    my_field = 'location'

@LibInline(Prop)
class PropInline(admin.TabularInline):
    my_field = 'group'

class LibAdmin(admin.ModelAdmin):
    
    #-- Lib List page
    
    list_display = ['name', 'project', 'status', 'description']
    list_editable = ['status']
    ordering = ['create_date']
    list_filter = ['project', 'priority', 'status', 'description', 'create_date']
    date_hierarchy = 'create_date'
    search_fields = ['name', 'project', 'status_name']
    
    #-- Lib Edit/Create page
    
    fieldsets = [
        (None,      {'fields': ['project', 'name', 'status', 'description']}),
        ('Tracking',{
                    'fields': [('priority', 'depend'), ('start_date', 'due_date')],
                    'classes': ['collapse']
                    }
        ),
    ]
    inlines = [CharInline, SetInline, PropInline]
    
    #-- Overrides
    
    def get_readonly_fields(self, request, obj=None):
        '''
        Set the name as read only unless we are creating
        a new one.
        '''
        readonly_fields = list(super(LibAdmin, self).get_readonly_fields(request, obj))
        #obj is None is case of creation page:
        if obj is not None:
            readonly_fields.extend(('project','name'))
        return readonly_fields

admin.site.register(Lib, LibAdmin)


#
#================ SEQUENCE
#
class ShotInline(admin.TabularInline):
    model = Shot
    fk_name = 'sequence'
    extra = 0
    fields = ['name', 'status', 'order']
    
    can_delete = False

class SequenceAdmin(admin.ModelAdmin):
    
    #-- Sequence List page
    
    list_display = ['name', 'project', 'status', 'order']
    list_editable = ['status','order']
    ordering = ['create_date']
    list_filter = ['project', 'priority', 'status', 'create_date']
    date_hierarchy = 'create_date'
    search_fields = ['name', 'project', 'status_name']
    
    #-- Sequence Edit/Create page
    
    fieldsets = [
        (None,      {'fields': ['project', 'name', 'status']}),
        ('Tracking',{
                    'fields': [('priority', 'depend'), ('start_date', 'due_date')],
                    'classes': ['collapse']
                    }
        ),
    ]
    inlines = [ShotInline]
    
    #-- Overrides
    
    def get_readonly_fields(self, request, obj=None):
        '''
        Set the name as read only unless we are creating
        a new one.
        '''
        readonly_fields = list(super(SequenceAdmin, self).get_readonly_fields(request, obj))
        #obj is None is case of creation page:
        if obj is not None:
            readonly_fields.extend(('project','name'))
        return readonly_fields

admin.site.register(Sequence, SequenceAdmin)

